package com.lin.mr.maizilaundry.fragmentpay;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lin.mr.maizilaundry.R;
import com.lin.mr.maizilaundry.activity.PayActivity;

/**
 * 余额支付页面
 */
public class FragmentRemain extends Fragment {
    private View view;
    private int count = 0;
    private TextView tv_remain_cost;
    private RelativeLayout rl_cover;
    //密码输入框
    private EditText et_password1, et_password2, et_password3, et_password4;
    private Button btn_remain_pay;
    private String password;
    private TextWatcher textWatcher;

    private FocusListeners focusListener;
    private KeyListeners keyListener;
    private OnEditTextListener onEditTextListener;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = View.inflate(getContext(), R.layout.fragment_remain, null);
        this.view = view;
        initView();
//        initEditText();
        return view;
    }

    private void initView() {
        tv_remain_cost = (TextView) view.findViewById(R.id.tv_remain_cost);
        rl_cover = (RelativeLayout) view.findViewById(R.id.rl_cover);
        et_password1 = (EditText) view.findViewById(R.id.et_password1);
        et_password2 = (EditText) view.findViewById(R.id.et_password2);
        et_password3 = (EditText) view.findViewById(R.id.et_password3);
        et_password4 = (EditText) view.findViewById(R.id.et_password4);
        btn_remain_pay = (Button) view.findViewById(R.id.btn_remain_pay);

        tv_remain_cost.setText(PayActivity.amount.substring(1) + ".00");//截取人民币符号后面的字符

        /**
         * 数字输入框有一个要求，密码是要一个一个按顺序输入的，所以让一个RelativeLayout遮在上面，让其依次获得焦点
         */
        rl_cover.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (password == null) {
                    et_password1.requestFocus();
                } else {
                    switch (password.length()) {
                        case 1:
                            et_password2.requestFocus();
                            break;
                        case 2:
                            et_password3.requestFocus();
                            break;
                        case 3:
                            et_password4.requestFocus();
                            break;
                    }
                }
                /**
                 * InputMethodManager:一个用于控制显示或隐藏输入法面板的类
                 */
                InputMethodManager m = (InputMethodManager) et_password1.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
//                这个方法在界面上切换输入法的功能，如果输入法出于显示状态，就将他隐藏，如果处于隐藏状态，就显示输入法。
                m.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);//弹出软键盘

            }
        });
//        focusListener = new FocusListeners();
        keyListener = new KeyListeners();
        initEditText(); //这个方法必须在这里调用否则报空指针
        //焦点改变监听
        et_password1.setOnFocusChangeListener(focusListener);
        et_password2.setOnFocusChangeListener(focusListener);
        et_password3.setOnFocusChangeListener(focusListener);
        et_password4.setOnFocusChangeListener(focusListener);

        //字符改变监听
        et_password1.addTextChangedListener(textWatcher);
        et_password2.addTextChangedListener(textWatcher);
        et_password3.addTextChangedListener(textWatcher);
        et_password4.addTextChangedListener(textWatcher);

        //删除按钮监听
        et_password1.setOnKeyListener(keyListener);
        et_password2.setOnKeyListener(keyListener);
        et_password3.setOnKeyListener(keyListener);
        et_password4.setOnKeyListener(keyListener);

        InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(et_password1.getWindowToken(), 0);
    }


    /**
     * 密码输入框焦点监听
     */
    class FocusListeners implements View.OnFocusChangeListener {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            switch (v.getId()) {
                case R.id.et_password1:
                    et_password1.requestFocus();
                    break;
                case R.id.et_password2:
                    et_password2.clearFocus();
                    et_password1.requestFocus();
                    break;
                case R.id.et_password3:
                    et_password3.clearFocus();
                    et_password4.requestFocus();
                    break;
            }
        }
    }

    /**
     * 字符改变监听
     */
    private void initEditText() {
        textWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            //输入框内容改变后调用
            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().length() == 1) {
                    if (et_password1.isFocused()) {
                        et_password2.setFocusable(true);
                        et_password2.requestFocus();
                        password = getEditString();
                        if (onEditTextListener != null) {
                            onEditTextListener.inputComplete(password);
                        }
                    } else if (et_password2.isFocused()) {
                        et_password3.setFocusable(true);
                        et_password3.requestFocus();
                        password = getEditString();
                        if (onEditTextListener != null) {
                            onEditTextListener.inputComplete(password);
                        }
                    } else if (et_password3.isFocused()) {
                        et_password4.setFocusable(true);
                        et_password4.requestFocus();
                        password = getEditString();
                        if (onEditTextListener != null) {
                            onEditTextListener.inputComplete(password);
                        }
                    } else if (et_password4.isFocused()) {
                        password = getEditString();
                        if (onEditTextListener != null) {
                            onEditTextListener.inputComplete(password);
                        }
                    }
                }
            }
        };
    }

    /**
     * 删除按钮监听
     */
    class KeyListeners implements View.OnKeyListener {

        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_DEL) {//如果按下的是键盘上的删除键
                //不知道不知道什么原因，点击一次删除按钮会调两次这个方法，所以处理一下，两次当一次
                count++;
                if (count < 2) {
                    return false;
                }
                count = 0;
                password = "";
                if (et_password4.isFocused()) {
                    et_password4.clearFocus();
                    et_password3.requestFocus();
                } else if (et_password3.isFocused()) {
                    et_password3.clearFocus();
                    et_password2.requestFocus();
                } else if (et_password2.isFocused()) {
                    et_password2.clearFocus();
                    et_password1.requestFocus();
                }
            }

            return false;
        }
    }

    /**
     * 拼凑用户输入的密码
     *
     * @return
     */
    public String getEditString() {
        String number = et_password1.getText().toString();
        number += et_password2.getText().toString();
        number += et_password3.getText().toString();
        number += et_password4.getText().toString();
        return number;
    }

    public interface OnEditTextListener {
        void inputComplete(String password);
    }
}
